package com.seal.commons.utils.security;

public class HexUtils {

    /**
     * 16进制数组数转化
     * 
     * @param hexStr
     * @return
     */
    public static byte[] toByte(String hexStr){
        byte digest[] = new byte[hexStr.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            String byteString = hexStr.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }
        return digest;
    }

    /**
     * 十六进制数转化
     * @param b
     * @return
     * @throws Exception
     */
    public static String toHexStr(byte b[]){
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }
        return hexString.toString();
    }
}
